import Vue from 'vue';
import Router from 'vue-router';
import store from '../store/store';
import { stringifyQuery, parseQuery } from './utils/query';
import { lsWrite, lsRead } from '../common/js/ls';
import * as types from '../store/type';
import Watermark from '@/common/js/watermark';

Vue.use(Router);
if (lsRead('user').userId) {
  store.commit(types.LOGIN, lsRead('user'));
}

let timeStr = new Date().getTime();
window.onhashchange = function (e) {
  let t = new Date().getTime() - timeStr;
  timeStr = new Date().getTime();
  console.log(`用户当前页面${e.oldURL}停留时间`, t);
};
if (!window.parseQuery || typeof window.parseQuery !== 'function') {
  window.parseQuery = parseQuery;
}

export const staticRouter = [
  {
    path: '/',
    meta: {
      watermark: false,
    },
    component: () => import('@/components/index.vue'),
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/components/404.vue'),
  },
  {
    path: '/login',
    meta: {
      watermark: false,
      _accessTime_: true,
    },
    name: 'login',
    component: () => import('@/components/login.vue'),
  },
  {
    path: '/refuel',
    name: 'refuel',
    component: () => import('@/components/layout/layout/'),
    children: [
      {
        path: 'logs',
        name: 'log',
        component: () => import('@/components/refuel'),
      },
      {
        path: 'station',
        name: 'station',
        component: () => import('@/components/gasStation'),
      },
      {
        path: 'showdown',
        name: 'showdown',
        component: () => import('@/components/showdown'),
      },
      {
        path: 'setMd',
        name: 'setMd',
        component: () => import('@/components/setMd'),
      },
      {
        path: 'virtual',
        name: 'virtual',
        component: () => import('@/components/virtualSelect'),
      },
      {
        path: 'vueoffice',
        name: 'vueoffice',
        component: () => import('@/components/vueOffice'),
      },
      {
        path: 'demoTable',
        name: 'demoTable',
        component: () => import('@/components/demoTable'),
      },
      {
        path: 'dimaRender',
        name: 'dimaRender',
        component: () => import('@/components/render/render.vue'),
      },
      {
        path: 'loginTencent',
        name: 'loginTencent',
        component: () => import('@/components/loginTencent.vue'),
      },
       {
        path: 'dragScaleDialog',
        name: 'dragScaleDialog',
        component: () => import('@/components/dragScaleDialogIndex.vue'),
      },
    ],
  },

  {
    path: '/child-map/*',
    name: 'map',
    component: () => import('@/components/layout/layout/'),
  },
  {
    path: '/child-log/*',
    name: 'log',
    component: () => import('@/components/layout/layout/'),
  },
];
export const router = new Router({
  // mode: "history",
  // base:"/",
  scrollBehavior: () => ({
    y: 0,
  }),
  stringifyQuery: stringifyQuery,
  parseQuery: parseQuery,
  routes: staticRouter,
});

// 置空路由信息
export const resetRouter = () => {
  const newRouter = router();
  router.matcher = newRouter.matcher;
};

router.beforeEach((to, from, next) => {
  console.log(from, to, next);
  if (to.meta._accessTime_ && to.query._accessTime_) {
    if (to.query._accessTime_ !== window.localStorage.getItem('_accessTime_')) {
      router.replace('/404');
    } else {
      next();
    }
  } else {
    next();
  }
});

router.afterEach(to => {
  if (store.state.user.license && to.meta.watermark != false) {
    Watermark.set(store.state.user.license);
  } else {
    Watermark.set('');
  }
});
